package com.zk.shop.service.Impl;

import com.zk.shop.dao.SortsDao;
import com.zk.shop.entity.Sorts;
import com.zk.shop.service.SortsService;
import com.zk.shop.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SortsServiceImpl
 * @Description TODO
 * @Author 来永刚
 * @Date 2019/9/29 0029下午 2:07
 * @Version 1.0
 **/
public class SortsServiceImpl implements SortsService {

    SqlSession session = SqlSessionFactoryUtil.getSqlSession(); //获取session
    SortsDao sd=session.getMapper(SortsDao.class); //创建SortsDao的对象

    /**
     * 增加分类
     * @param sorts
     * @return boolean
     */
    @Override
    public boolean addSorts(Sorts sorts) {
        boolean flag = false;
        try {
            flag=sd.addSorts(sorts);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    /**
     *删除分类
     * @param sortsId
     * @return boolean
     */
    @Override
    public boolean deleteSorts(int sortsId) {
        boolean flag = false;
        try {
             flag = sd.deleteSorts(sortsId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    /**
     * 显示所有的分类
     * @return List<Sorts>
     */
    @Override
    public List<Sorts> showSorts() {
        List<Sorts> sortsList = new ArrayList<>();
        try {
           sortsList= sd.showSorts();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return sortsList;
    }

    /**
     * 修改分类
     * @param sorts
     * @return boolean
     */
    @Override
    public boolean updateSorts(Sorts sorts) {
        boolean flag = false;
        try {
            flag = sd.updateSorts(sorts);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }
}
